package com.originskyseed.platform.user.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.common.api.util.QueryAppUtil;
import com.originskyseed.platform.common.utils.JsonUtils;
import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.user.i18n.UserResponseMsg;
import com.originskyseed.platform.user.model.AnchorRelateFans;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.IAnchorRelateFansService;
import com.originskyseed.platform.user.vo.request.AnchorRelateFansRequestVO;

/**
 * 主播粉丝信息表Controller类（OSI_ANCHOR_RELATE_FANS）
 * 
 * @author Tangwanghui
 */
@Controller
@RequestMapping("/anchorRelateFans")
@BaseLocaleResource(localeFileName = "i18n_user")
public class AnchorRelateFansController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(AnchorRelateFansController.class);

	@DubboReference
	private IAnchorRelateFansService anchorRelateFansService;

	/**
	 * 分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/anchorRelateFansList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<AnchorRelateFans>> anchorRelateFansList(HttpServletRequest request) {
		BaseResponseBody<BasePage<AnchorRelateFans>> result = null;
		try {
			// 请求对象
			AnchorRelateFansRequestVO requestVO = this.getParameterBean(AnchorRelateFansRequestVO.class);
			// 请求接口
			TreeMap<String, Object> paramMap = new TreeMap<>();
			paramMap.put("pageNum", requestVO.getPageNo());
			paramMap.put("pageSize", requestVO.getPageSize());
			if (StringUtils.isNotBlank(requestVO.getPlayerId())) {
				paramMap.put("playerId", requestVO.getPlayerId());
			}
			if (StringUtils.isNotBlank(requestVO.getPlayerNickName())) {
				paramMap.put("playerNickName", requestVO.getPlayerNickName());
			}
			if (StringUtils.isNotBlank(requestVO.getAnchorId())) {
				paramMap.put("anchorId", requestVO.getAnchorId());
			}
			if (StringUtils.isNotBlank(requestVO.getPlayerId())) {
				paramMap.put("anchorNickName", requestVO.getAnchorNickName());
			}
			if (StringUtils.isNotBlank(requestVO.getStartTime())) {
				paramMap.put("startTime", requestVO.getStartTime());
			}
			if (StringUtils.isNotBlank(requestVO.getEndTime())) {
				paramMap.put("endTime", requestVO.getEndTime());
			}
			BasePage<AnchorRelateFans> basePage = new BasePage<>(requestVO.getPageNo(), requestVO.getPageSize());

			Map resultMap = QueryAppUtil.query("queryFocusAnchorUrl", 1, paramMap);
			Integer code = Integer.parseInt(resultMap.get("code").toString());
			String msg = (String) resultMap.get("msg");
			if (code == 1) {
				List<AnchorRelateFans> anchorRelateFansList = new ArrayList<>();
				// json字符串转jsonArray
				String data = (String) resultMap.get("data");
				anchorRelateFansList = JsonUtils.parseArray(data, AnchorRelateFans.class);

				Integer totalNum = (Integer) resultMap.get("totalNum");
				if (totalNum != null) {
					Integer pageCount = 0; // 页数
					if (totalNum % requestVO.getPageSize() == 0) {
						pageCount = totalNum / requestVO.getPageSize();
					} else {
						pageCount = totalNum / requestVO.getPageSize() + 1;
					}
					basePage.setTotalRecord(totalNum);
					basePage.setTotalPage(pageCount);
				}
				basePage.setResults(anchorRelateFansList);
			} else {
				return new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), msg, null);
			}

			result = new BaseResponseBody<BasePage<AnchorRelateFans>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), basePage);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<AnchorRelateFans>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/anchorRelateFansSave.json", method = RequestMethod.POST)
	@RequiredParams(names = {})
	public BaseResponseBody<AnchorRelateFans> anchorRelateFansSave(HttpServletRequest request) {
		BaseResponseBody<AnchorRelateFans> result = null;
		try {
			// 请求对象
			AnchorRelateFans anchorRelateFans = this.getParameterBean(AnchorRelateFans.class);

			if (StringUtils.isNotBlank(anchorRelateFans.getId())) {
				this.anchorRelateFansService.updateByPrimaryKey(anchorRelateFans);
			} else {
				this.anchorRelateFansService.insertSelective(anchorRelateFans);
			}
			result = new BaseResponseBody<AnchorRelateFans>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_SUCCESS.getMsg()), anchorRelateFans);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<AnchorRelateFans>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/anchorRelateFansDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> anchorRelateFansDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.anchorRelateFansService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 查询关注主播列表
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectAnchorRelateFansList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody selectAnchorRelateFansList(HttpServletRequest request) {
		BaseResponseBody result = null;
		TreeMap<String, Object> paramMap = new TreeMap<>();
		try {
			// 用户信息
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			// 请求对象
			AnchorRelateFansRequestVO requestVO = this.getParameterBean(AnchorRelateFansRequestVO.class);
			// 请求接口
			paramMap.put("pageNum", requestVO.getPageNo());
			paramMap.put("pageSize", requestVO.getPageSize());
			if (StringUtils.isNotBlank(requestVO.getPlayerId())) {
				paramMap.put("playerId", requestVO.getPlayerId());
			}
			if (StringUtils.isNotBlank(requestVO.getPlayerNickName())) {
				paramMap.put("playerNickName", requestVO.getPlayerNickName());
			}
			if (StringUtils.isNotBlank(requestVO.getStartTime())) {
				paramMap.put("startTime", requestVO.getStartTime());
			}
			if (StringUtils.isNotBlank(requestVO.getEndTime())) {
				paramMap.put("endTime", requestVO.getEndTime());
			}
			/*if (userInfo.getType()==11 || StringUtils.isNotBlank(userInfo.getOtherAccount())) {
				paramMap.put("anchorId", userInfo.getOtherAccount());
			}*/

			if (StringUtils.isNotBlank(requestVO.getAnchorId())) {
				paramMap.put("anchorId", requestVO.getAnchorId());
			}
			if (StringUtils.isNotBlank(requestVO.getAnchorNickName())) {
				paramMap.put("anchorNickName", requestVO.getAnchorNickName());
			}
			BasePage<AnchorRelateFans> basePage = new BasePage<>(requestVO.getPageNo(), requestVO.getPageSize());

			Map resultMap = QueryAppUtil.query("queryFocusAnchorUrl", 1, paramMap);
			Integer code = Integer.parseInt(resultMap.get("code").toString());
			String msg = (String) resultMap.get("msg");
			if (code == 1) {
				List<AnchorRelateFans> anchorRelateFansList = new ArrayList<>();
				// json字符串转jsonArray
				String data = (String) resultMap.get("data");
				anchorRelateFansList = JsonUtils.parseArray(data, AnchorRelateFans.class);

				Integer totalNum = (Integer) resultMap.get("totalNum");
				if (totalNum != null) {
					Integer pageCount = 0; // 页数
					if (totalNum % requestVO.getPageSize() == 0) {
						pageCount = totalNum / requestVO.getPageSize();
					} else {
						pageCount = totalNum / requestVO.getPageSize() + 1;
					}
					basePage.setTotalPage(pageCount);
				}
				basePage.setTotalRecord(totalNum);
				basePage.setResults(anchorRelateFansList);
			} else {
				return new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), msg, null);
			}

			result = new BaseResponseBody<BasePage<AnchorRelateFans>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), basePage);
		} catch (Exception e) {
			logger.error("==selectAnchorRelateFansList="+paramMap);
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}
}